<template>
  <div id="total">
    <!-- 上部分 -->
    <div id="top">
      <div class="storage">
        <!-- 制单号输入框 -->
        <div id="input">
          <!-- elementUi -->
          <el-form ref="searchFormRef" :inline="true" :model="searchData">
            <el-form-item prop="username" label="制单号">
              <el-input
                v-model="searchData.id"
                placeholder="请输入制单号"
                @blur="queryId"
              />
            </el-form-item>
          </el-form>
        </div>

        <!-- 审核状态 -->
        <div id="audit">
          <div id="num">
            <span>审核状态:</span>
          </div>
          <!-- elementUi -->
          <el-select
            v-model="auditValue"
            class="m-2"
            placeholder="全部"
            size="large"
            @change="auditFn()"
          >
            <el-option
              v-for="item in auditOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>

        <!-- 供应商 -->
        <div id="supplier">
          <div id="num">
            <span>供应商:</span>
          </div>
          <!-- elementUi -->
          <el-select
            v-model="value1"
            class="m-2"
            placeholder="全部"
            size="large"
            @change="supplierFn()"
          >
            <el-option
              v-for="item in getSupplierData2"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </div>

        <!-- 采购日期 -->
        <div class="block">
          <div id="num">
            <span class="demonstration">采购日期:</span>
          </div>
          <el-date-picker
            v-model="value2"
            type="daterange"
            range-separator="To"
            start-placeholder="起始日期"
            end-placeholder="截止日期"
            size="large"
            @change="dataPicker"
          />
        </div>
      </div>
    </div>
    <!-- 下部分 -->
    <div id="bottom">
      <!-- 表格 -->
      <el-table
        :data="getBillData"
        style="width: 100%"
        @current-change="formChange"
      >
        <el-table-column type="index" width="80" />
        <el-table-column align="center" prop="id" label="制单号" width="200" />
        <el-table-column
          align="center"
          prop="purchaser"
          label="采购人"
          width="120"
        />
        <el-table-column
          align="center"
          prop="allTrade"
          label="采购金额"
          width="100"
        />
        <el-table-column
          align="center"
          prop="purchTime"
          label="采购日期"
          width="182"
        />
        <el-table-column
          align="center"
          prop="checker"
          label="审核人"
          width="120"
        />
        <el-table-column
          align="center"
          prop="checktime"
          label="审核日期"
          width="200"
        />
        <el-table-column
          align="center"
          prop="supName"
          label="供应商"
          width="120"
        />
        <el-table-column align="center" label="状态" width="140"
          ><template #default="scope">
            <span v-if="scope.row.status">已审核</span>
            <span v-if="!scope.row.status">未审核</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="260">
          <template #default="scope">
            <el-button @click="ViewFn(scope.row)" type="primary"
              >查看</el-button
            >
            <el-button @click="StorageFn(scope.row)" type="primary"
              >再次入库</el-button
            >
            <el-button @click="deleteFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div id="pager-wapper">
        <el-pagination
          :page-sizes="[2, 3, 5, 10]"
          id="paging"
          background
          layout="prev, pager, next"
          v-model:page-size="pageSize"
          :total="store.state.process.billTotal"
          @current-change="pageChange"
        />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import router from "@/router";
import supplier from "@/store/medicine/supplier";
import { User } from "@element-plus/icons-vue";
import { ElMessage, ElTable } from "element-plus";
import { ref, reactive, defineComponent, onBeforeMount, computed } from "vue";
import { mapGetters, mapMutations, useStore } from "vuex";
const store = useStore();
// 查看按钮传送数据
const sendData = mapMutations(["sendData"]).sendData.bind({
  $store: store,
});

// 再次入库按钮跳转页面
const StorageFn = (data: any) => {
  sendData(data);
  router.push({
    path: "/medicine/process/warehousing",
  });
};
// 展示默认数据
const setBillData = mapMutations(["setBillData"]).setBillData.bind({
  $store: store,
});
// 查看按钮
const setOrderData = mapMutations(["setOrderData"]).setOrderData.bind({
  $store: store,
});
// 根据制单号删除订单
const setDeleteList = mapMutations(["setDeleteList"]).setDeleteList.bind({
  $store: store,
});
// 根据id查询单据(制单号)
const setQueryData = mapMutations(["setQueryData"]).setQueryData.bind({
  $store: store,
});
// 根据id查询单据(制单号)
const queryId = () => {
  // console.log(searchData.id);
  setQueryData({ id: searchData.id });
};

// 查看按钮跳转页面
const ViewFn = (data: any) => {
  sendData(data);
  // console.log(data);
  // console.log("1!!!",data.id);
  // 根据id查询数据
  setOrderData({ billId: data.id, currentPage: 1, pageSize: pageSize.value });
  router.push({
    path: "/medicine/process/view",
  });
  setQueryData({ id: data.id });
};

// 根据制单号删除订单
const deleteFn = (row: any) => {
  // console.log("11111", row.id);
  setDeleteList({
    id: row.id,
  });
  // 刷新页面
  // 弹窗
  ElMessage({
    message: "删除成功",
    type: "success",
  });
  setBillData({ currentPage: 1, pageSize: pageSize.value });
};
// 审核状态下拉框
const setAuditList = mapMutations(["setAuditList"]).setAuditList.bind({
  $store: store,
});
// 供应商状态下拉框
const setSupplierList = mapMutations(["setSupplierList"]).setSupplierList.bind({
  $store: store,
});
// 展示默认数据
const getBillData = computed(
  mapGetters(["getBillData"]).getBillData.bind({
    $store: store,
  })
);
// 审核状态
const auditValue = ref("");
const auditOptions = [
  {
    value: "",
    label: "全部",
  },
  {
    value: "1",
    label: "已审核",
  },
  {
    value: "0",
    label: "未审核",
  },
];
const auditFn = () => {
  // console.log(auditValue.value);
  console.log("供应商", value1.value);

  setBillData({
    currentPage: 1,
    pageSize: pageSize.value,
    status: auditValue.value,
    supName: value1.value,
  });
};
// 供应商下拉框
const getSupplierData2 = computed(
  mapGetters(["getSupplierData2"]).getSupplierData2.bind({
    $store: store,
  })
);
// 供应商下拉框
const supplierFn = () => {
  console.log("供应商", value1.value);
  setBillData({
    currentPage: 1,
    pageSize: pageSize.value,
    status: auditValue.value,
    supName: value1.value,
  });
};
// 分页
// 默认为5页
const pageSize = ref(5);
onBeforeMount(() => {
  setBillData({ currentPage: 1, pageSize: pageSize.value });
  // 审核状态下拉框
  setAuditList(); //
  // 供应商下拉框
  setSupplierList(); //
});
// 分页功能实现
const pageChange = (currentPage: any) => {
  setBillData({ currentPage: currentPage, pageSize: pageSize.value });
};
const searchData = reactive({
  id: "",
});
// 供应商选择器
const value1 = ref("");
// 采购日期
const value2 = ref("");
// 日期选择器
const dataPicker = () => {
  console.log("日期选择器", value2.value[0]);
  console.log("日期选择器", value2.value[1]);
  // 更改时间戳函数
  const formatDate = (a: any) => {
    var date = new Date(a);
    var YY = date.getFullYear() + "-";
    var MM =
      (date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1) + "-";
    var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return YY + MM + DD + " 11:59:59";
  };
  // 起始时间
  let startDate = value2.value[0];
  // 结束时间
  let endDate = value2.value[1];
  let startDate2 = formatDate(startDate);
  let endDate2 = formatDate(endDate);
};
setBillData({
  
});
const formChange = () => {};
</script>
<style scoped>
#top #bottom {
  margin-top: 20px;
}
.storage {
  display: flex;
  align-items: center;
  height: 40px;
  background-color: white;
  margin-bottom: 20px;
}
/* 制单号输入框 */
#input {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 300px;
  height: 200px;
  margin-top: 17.5px;
  margin-left: 32px;
}
#num {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100px;
}
/* 审核状态选择器 */
#audit {
  display: flex;
  width: 260px;
}
/* 供应商 */
#supplier {
  display: flex;
  width: 260px;
}
.block {
  display: flex;
  width: 550px;
}
#paging {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
.mb-4 {
  margin-top: 20px;
}
.pager-wapper {
  justify-content: center;
  align-items: center;
}
#pager-wapper {
  background-color: white;
}
</style>
